package org.ehe.business.engineer.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.ehe.business.engineer.domain.EngineerEntity;
import org.ehe.business.engineer.domain.bo.EngineerBO;
import org.ehe.business.engineer.domain.vo.EngineerEntityVO;
import org.ehe.business.engineer.service.EngineerService;
import org.ehe.business.project.domain.ProjectAssignmentEntity;
import org.ehe.business.project.service.ProjectService;
import org.ehe.common.core.domain.R;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.ehe.common.satoken.utils.LoginHelper;
import org.ehe.common.core.BaseController;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 工程师管理
 *
 * @author :zhangnn
 * @className :EngineerController
 * @description:
 * @date 2025-07-16 09:28:25
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/engineer")
public class EngineerController extends BaseController {

    private final EngineerService engineerService;
    private final ProjectService projectService;

    /**
     * 列表
     *
     * @return
     */
    @PostMapping("/list")
    public TableDataInfo<EngineerEntityVO> getAllEngineers(EngineerBO engineerBO, PageQuery query) {
        return engineerService.getAllEngineers(engineerBO,query);
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @SaCheckPermission("project:engineer:view")
    @GetMapping("/{id}")
    public R<EngineerEntity> getEngineer(@PathVariable Long id) {
        return R.ok(engineerService.getEngineerDetail(id));
    }

    /**
     * 创建
     * @param request
     * @return
     */
    @SaCheckPermission("project:engineer:add")
    @Log(title = "新增工程师", linkType = "engineer", businessType = BusinessType.INSERT, idField = "id", nameField = "name")
    @PostMapping("/create")
    public R<Void> createEngineer(@RequestBody EngineerEntity request) {
        request.setCreateBy(LoginHelper.getUsername());
        request.setDelFlag("0");
        request.setTenantId(LoginHelper.getTenantId());
        return toAjax(engineerService.createEngineer(request));
    }

    /**
     * 修改
     * @param request
     * @return
     */
    @SaCheckPermission("project:engineer:edit")
    @Log(title = "修改工程师信息", linkType = "engineer", businessType = BusinessType.UPDATE, idField = "id", nameField = "name")
    @PostMapping("/update")
    public R<Void> updateEngineer(@RequestBody EngineerEntity request) {
        request.setCreateBy(LoginHelper.getUsername());
        request.setDelFlag("0");
        request.setTenantId(LoginHelper.getTenantId());
        return toAjax(engineerService.updateEngineer(request));
    }


    /**
     * 分配项目列表
     * @param id
     * @return
     */
    @SaCheckPermission("project:engineer:check")
    @GetMapping("/{id}/projects")
    public TableDataInfo<ProjectAssignmentEntity> getAllEngineers(@PathVariable Long id,PageQuery query) {
        return projectService.getEngineerProjects(id,query);
    }


    /**
     * 删除工程师信息
     */
    @SaCheckPermission("project:engineer:remove")
    @Log(title = "工程师信息删除", linkType = "engineer", businessType = BusinessType.DELETE, idField = "id", nameField = "name")
    @DeleteMapping("/{id}")
    public R<Boolean> deleteEngineer(@PathVariable Long id) {
        return R.ok(engineerService.removeById(id));
    }


}
